#!/bin/bash

CATAPULT_MODEL=$1
CATAPULT_INSTANCE=$2

CATAPULT_DIR=../catapult_`echo $CATAPULT_MODEL`
CATAPULT_EXE_NAME=catapult.out
CATAPULT_EXE=`echo $CATAPULT_DIR`/`echo $CATAPULT_EXE_NAME`
CATAPULT_TEXTURES_DIR_NAME=textures
CATAPULT_TEXTURES_DIR=../`echo $CATAPULT_TEXTURES_DIR_NAME`
CATAPULT_INSTANCE_DIR=../catapult_instance_`echo $CATAPULT_MODEL`_`echo $CATAPULT_INSTANCE`

CATAPULT_BUILD_SCRIPT=build_catapult.sh


if [ "$CATAPULT_MODEL" == "" ] || [ "$CATAPULT_INSTANCE" == "" ]; then
  echo "usage: ./run_catapult.sh <model> <instance>"
  exit
fi

if [ ! -d $CATAPULT_DIR ]; then
  echo "catapult directory does not exist corresponding to the specific model."
  echo "  - catapult_dir: $CATAPULT_DIR"
  exit
fi

if [ ! -f $CATAPULT_EXE ]; then
  echo "build catapult execution file."
  echo "  - catapult_exe: $CATAPULT_EXE"
  ./`echo $CATAPULT_BUILD_SCRIPT` `echo $CATAPULT_MODEL`
fi

if [ ! -d $CATAPULT_INSTANCE_DIR ]; then
  echo "create catapult instance directory."
  echo "  - catapult_instance_dir: $CATAPULT_INSTANCE_DIR"
  mkdir $CATAPULT_INSTANCE_DIR
fi

cd $CATAPULT_INSTANCE_DIR

if [ ! -L $CATAPULT_TEXTURES_DIR_NAME ]; then
  echo "create link to catapult textures directory."
  echo "  - catapult_textures_dir: $CATAPULT_TEXTURES_DIR"
  ln -s $CATAPULT_TEXTURES_DIR $CATAPULT_TEXTURES_DIR_NAME
fi

echo "remove message files."
rm -f *.msg

echo "launch catapult simulator."
echo "  - model:    $CATAPULT_MODEL"
echo "  - instance: $CATAPULT_INSTANCE"
./`echo $CATAPULT_EXE`

cd -

